package data;

import gui.Screen;
import java.util.*;

// BankDatabase.java
// Represents the bank account information database 

public class BankDatabase{

	//private Account accounts[]; // array of Accounts
	public ArrayList<User> userList; // array of User
	private int choice3;
	private Screen screen = new Screen();

	// no-argument BankDatabase constructor initializes accounts
	public BankDatabase() {
		userList = new ArrayList<User>();
	} // end no-argument BankDatabase constructor
	
	// retrieve Account object containing specified account number
	public User getUsers (int CPR) {
		// loop through accounts searching for matching account number
		for (User currentAccount : userList) {
			// return current account if match found
			if ( currentAccount.getCPR() == CPR )
				return currentAccount;
		} // end for

		return null; // if no matching account was found, return null
	} // end method getAccount

	// determine whether user-specified account number and PIN match
	// those of an account in the database
	public boolean authenticateUser(int userAccountNumber, int userPIN){
		// attempt to retrieve the account with the account number
		User userAccount = getUsers( userAccountNumber );
		System.out.println("CPR IS" + userAccount.getCPR());
		// if account exists, return result of Account method validatePIN
		if (userAccount != null)
			return userAccount.validatePIN(userPIN);
		else
			return false; // account number not found, so return false
	} // end method authenticateUser

	public int getCurrentAccount(int choice2) {
		choice3 = choice2;
		return choice2 - 1;
	}

	public int chooseAccount() {
		int choice1 = getCurrentAccount(choice3);
		return choice1;
	}

	public void addUser(User _account) {
		this.userList.add(_account);
	}

	public User getSpecificAccount(int index) {
		return userList.get(index);
	}

	// return available balance of Account with specified account number
	public double getAvailableBalance( int userAccountNumber ) {
		return getUsers( userAccountNumber ).getAccountList().get(chooseAccount()).getAvailableBalance();
	} // end method getAvailableBalance

	// return total balance of Account with specified account number
	public double getTotalBalance( int userAccountNumber ) {
		return getUsers( userAccountNumber ).getAccountList().get(chooseAccount()).getTotalBalance();
	} // end method getTotalBalance

	// credit an amount to Account with specified account number
	public void credit( int userAccountNumber, double amount ) {
		getUsers( userAccountNumber).getAccountList().get(chooseAccount()).credit(amount);
	} // end method credit

	// debit an amount from Account with specified account number
	public void debit( int userAccountNumber, double amount ) {
		getUsers( userAccountNumber ).getAccountList().get(chooseAccount()).debit(amount);
	} // end method debit

	public final Iterator<User> displayAccount() {
		//gets the list of accounts from the bankdatabase
		Iterator<User> listIterator = userList.iterator();
		// while loop ends when there are no more acounts to display
		while(listIterator.hasNext()) {
			User value = listIterator.next(); // chooses next account in our arraylist
			System.out.println( "User number (Login): " + value.getCPR() + " PIN " + value.getPin()
					+ "  Name ");
		}
		return listIterator; 
	}

} // end class BankDatabase
